package org.mathcloud.math.core.generic.operator.algebra;

import org.mathcloud.math.core.common.operator.BinaryOperator;
import org.mathcloud.math.core.common.quantity.ComplexNumber;
import org.mathcloud.math.core.common.quantity.RealNumber;
import org.mathcloud.math.core.common.signature.QuantityObjectSignature;
import org.mathcloud.math.core.generic.operator.GenericBinaryOperator;
import org.mathcloud.math.core.generic.quantity.GenericComplexNumber;
import org.mathcloud.math.core.generic.quantity.GenericRealNumber;


public class GenericDivide extends GenericBinaryOperator implements
		BinaryOperator
{
	public GenericDivide()
	{
		super();
		signature.setMMLContentTag("divide");
	}
	
	@Override
	public void calculate()
	{
		QuantityObjectSignature resSign = getResultSignature(); 
		if (resSign!= null)
		{
			if (resSign.isRealNumberSignature())
			{
				if (((RealNumber)operand2).getValue()!=0)
					res = new GenericRealNumber(((RealNumber)operand1).getValue()/((RealNumber)operand2).getValue());					
			}
			else if (resSign.isComplexNumberSignature())
			{
	      double mod, arg;
	      mod = arg = 0;

	      if (operand1.getSignature().isRealNumberSignature())
	      {
	      	mod = ((RealNumber)operand1).getValue();
	      } else if (operand1.getSignature().isComplexNumberSignature())
	      {
	        mod = ((ComplexNumber)operand1).getMod();
	        arg = ((ComplexNumber)operand1).getArg();
	      }
	      if (operand2.getSignature().isRealNumberSignature())
	      {
	        if (((RealNumber)operand2).getValue()!=0)
	        	mod = mod / ((RealNumber)operand2).getValue();
	      } else if (operand2.getSignature().isComplexNumberSignature())
	      {
	      	if (((ComplexNumber)operand2).getMod()!=0)
	      	{
		      	mod = mod / ((ComplexNumber)operand2).getMod();
		        arg = arg - ((ComplexNumber)operand2).getArg();		      		
	      	}
	      	else return;
	      }
	      res = new GenericComplexNumber(0,0);
	      ((GenericComplexNumber)res).setMod(mod);
	      ((GenericComplexNumber)res).setArg(arg);					
			}
		}
	}	
}
